// pages/update/update.js

let app=getApp();
let touxiang=null;
Page({
//密码与确认密码暂时未弄
    // 页面的初始数据
    data: {
        showplace:false,
        showbutton:true,
        showusername:false,
        showpassword:false,
        showphone:false,
        showbanji:false,
        showcity:false,
        showgender:false,
        showhonor:false,
        showbirth:false,
        showtouxiang:false,
        username:null,
        password:null,
        phone:null,
        city:null,
        honor:null,
        banji:null,
        birth:"请选择你的生日",
        xingbie:null,
        yanzheng:null,
        opassword:null,
        qpassword:null,
        openid:null,
        jtouxiang:null,
        imagurl:null,
        region: ['选择', '你的', '地区'],
        customItem: '全部',
        dialogShow: false,
        buttons: [{text: '男'}, {text: '女'}]

    },
    //地址
    bindRegionChange: function (e) {
        this.setData({
          region: e.detail.value
        })
        this.data.city=this.data.region[0]+this.data.region[1]+this.data.region[2];
      },
    //性别
      openConfirm: function () {
        this.setData({
            dialogShow: true
        })
    },
    tapDialogButton(e) {
        this.setData({
            dialogShow: false,
            showOneButtonDialog: false
        })
        console.log(e.detail.item);
        if(e.detail.item.text=="男")
        {
            this.setData({
                gender:"男"
            })
            this.getgender(1);
        }
        else{
            this.setData({
               gender:"女"
            })
            this.getgender(0);
        }
    },
    getgender(g)
{
    this.data.xingbie=g;
},
//生日
bindDateChange: function (e) {
    this.setData({
      birth: e.detail.value
    })
  },
    
//更换头像
updatetouxiang()
{
    let that=this;
    wx.chooseImage({
        sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
        sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
        success: function (res) {
            // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
            console.log(res.tempFilePaths[0]);
            that.data.jtouxiang=res.tempFilePaths[0];
            that.uploadfile();
        }
    })
},
//上传头像文件
uploadfile()
{
    let that=this;
    wx.uploadFile({
        url: app.globalData.url+"/upload",
        filePath: that.data.jtouxiang,
        name: 'file',
        formData: 
        {
        },
        header: {  
          "Content-Type": "multipart/form-data"  
        },
        timeout: 10000,
        success: (res) => {
          const data = JSON.parse(res.data)
        let touxiang=data.data;
         wx.request({
            url:app.globalData.url+'/updateavstar', 
            header: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8' },
            data: {
                openid:that.data.openid,
                avstar:touxiang
             },
             method: 'post',
             success: function (res) {
                 wx.setStorageSync('touxiang', touxiang);
                  app.globalData.touxiang=touxiang;
        wx.redirectTo({
          url: '/pages/info/info',
        })
             },
             fail:function(err){//请求失败之后要做什么
                console.log('请求失败')
                }
                
        })
        
        }
    })
    
},

    // 生命周期函数--监听页面加载
    //获取本地用户值
    getuserinfo()
    {
        this.data.openid=app.globalData.openid;
        this.setData({
            imagurl:app.globalData.touxiang
        })
    },
    onLoad: function (options) {
        if( options.name==2)
        {
            this.setData({
                showusername:true
            })
        }
        else if( options.name==3)
        {
            this.setData({
                showgender:true
            })
        }
        else if( options.name==4)
        {
            this.setData({
                showbanji:true
            })
        }
        else if( options.name==5)
        {
            this.setData({
                showphone:true
            })
        }
        else if( options.name==6)
        {
            this.setData({
                showpassword:true
            })
        }
        else if( options.name==7)
        {
            this.setData({
                showbirth:true
            })
        }
        else if( options.name==8)
        {
            this.setData({
                showcity:true
            })
        }
        else if( options.name==9)
        {
            this.setData({
                showhonor:true
            })
        }
        else if( options.name==1)
        {
            this.setData({
                showbutton:false,
                showtouxiang:true
            })
        }
        this.getuserinfo();
          
    },
    //获取修改的用户名
   getusername(e)
   {
       this.data.username=e.detail.value;
   },
//获取修改的荣耀值
   gethonor(e)
{
    this.data.honor=e.detail.value;
},
//获取修改的生日
getbirth(b)
{
    this.data.birth=b.detail.value;
},

//获取修改的班级
getbanji(b)
{
    this.data.banji=b.detail.value;
},
//获取修改的城市
getcity(c)
{
    this.data.city=c.detail.value;
},
//获取修改的手机号
getphone(p)
{
    if (!(/^((13[0-9])|(14[0-9])|(15[0-9])|(17[0-9])|(18[0-9]))\d{8}$/.test(p.detail.value))) {
        wx.showToast({
        title: '手机号码有误',
        duration: 1000,
        icon:'none'
        });
    }
    else{
        wx.showToast({
            title: '手机号码正确',
            duration: 1000,
            icon:'none'
            });
        this.data.phone=p.detail.value;
    }
},
//获取修改的验证码
getyanzheng(y)
{
    this.data.yanzheng=y.detail.value;
},
//获取后台验证码
getmima()
{

},
//获取旧密码
getopassword(o)
{
    this.data.opassword=o.detail.value;
    console.log(this.data.opassword);
},
//获取新密码
gopassword(o)
{
    this.data.password=o.detail.value;
},
//获取确认密码
goqpassword(o)
{
    this.data.qpassword=o.detail.value;
},
//向后台提交更新信息
updateinfo()
{
    let that=this;
    wx.request({
        url:app.globalData.url+'/update', 
        header: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8' },
        data: {
            openid:that.data.openid,
            xingbie:that.data.xingbie,
            username:that.data.username,
            password:that.data.password,
            phone:that.data.phone,
            birth:that.data.birth,
            city:that.data.city,
            honor:that.data.honor,
            banji:that.data.banji
         },
         method: 'get',
         success: function (res) {
            wx.setStorageSync('username',that.data.username);
    wx.redirectTo({
      url: '/pages/info/info',
    })
         },
         fail:function(err){//请求失败之后要做什么
            console.log('请求失败')
            }
            
    })
},
})